﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using StudentWeb.DAL;
using StudentWeb.Model;
using StudentWeb.Common;

namespace StudentWeb.BLL
{
    public class AdminsBO
    {
        #region 管理员信息 登录 
        public IResult Login(string AdminUsername, string AdminPassword)
        {
            var list = new AdminsDataDAO().GetList();
            var adminsList = list.Where(item => item.AdminUsername == AdminUsername).ToList();//取对应用户名列表
            if (adminsList.Count < 1)//如该用户记录不存在 
            {
                return new Result(ResultCode.Fail, "用户名不存在");
            }
            if (adminsList[0].AdminPassword != AdminPassword)//判断密码 
            {
                return new Result(ResultCode.Fail, "密码错误");
            }
            return new Result(ResultCode.Success, "登录成功");
        }
        #endregion
        #region 管理员信息 注册 
        public bool SaveAdminsInfo(dynamic data)
        {
            int AdminID = data.AdminID ?? 0;
            var result = 0;
            if (AdminID > 0)
            {
                var record = new AdminsDataDAO().GetInfo(AdminID);
                record.AdminUsername = data.AdminUsername;
                record.AdminPassword = data.AdminPassword;
                record.AdminPhone = data.AdminPhone;
                record.AdminRegistrationDate = data.AdminRegistrationDate;
                result = new AdminsDataDAO().Update(record);
            }
            else if (AdminID == 0)
            {
                var user = new AdminsData()
                {
                    AdminUsername = data.AdminUsername,
                    AdminPassword = data.AdminPassword,
                    AdminPhone = data.AdminPhone,
                    AdminRegistrationDate = data.AdminRegistrationDate,
                };
                result = new AdminsDataDAO().Insert(user);
            }
            return result > 0;
        }
        #endregion
        #region 管理员信息 全部记录
        public IResult GetAdminsList()
        {
            var list = new AdminsDataDAO().GetList();
            return new DataResult(list);
        }
        #endregion
        #region 管理员信息 单条记录
        public IResult GetAdminInfo(int AdminID)
        {
            var record = new AdminsDataDAO().GetInfo(AdminID);
            return new DataResult(record);
        }
        #endregion
        #region 管理员信息 添加
        public bool InsertAdminsInfo(dynamic data)
        {
            var user = new AdminsData()
            {
                AdminUsername = data.AdminUsername,
                AdminPassword = data.AdminPassword,
                AdminPhone = data.AdminPhone,
                AdminRegistrationDate = data.AdminRegistrationDate
            };
            var result = new AdminsDataDAO().Insert(user);
            return result > 0;
        }
        #endregion
        #region 管理员信息 修改
        public bool UpdateAdminsInfo(dynamic data)
        {
            int AdminID = data.AdminID;
            var record = new AdminsDataDAO().GetInfo(AdminID);
            record.AdminUsername = data.AdminUsername;
            record.AdminPassword = data.AdminPassword;
            record.AdminPhone = data.AdminPhone;       
            record.AdminRegistrationDate = data.AdminRegistrationDate;
            var result = new AdminsDataDAO().Update(record);
            return result > 0;
        }
        #endregion
        #region 管理员信息 删除
        public IResult DeleteAdiminsInfo(int AdminID)
        {
            var res = new AdminsDataDAO().Delete(AdminID);
            if (res > 0)
            {
                return new Result(ResultCode.Success, "删除成功");
            }
            else
            {
                return new Result(ResultCode.Fail, "删除失败");
            }
        }
        #endregion
        #region 管理员信息 分页列表
        public IResult PageAdminsList(int pageIndex, int pageSize, string wc = null)
        {
            var list = new AdminsDataDAO().GetList(pageIndex, pageSize, wc);
            return new DataResult(list);
        }
        #endregion
    }
}
